﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Membership;

namespace Ue.Qcdz.UI.Controls.Membership
{
    public class TheLogin : SkinnedControl
    {
        private string skinFileName = "~/Themes/zh-cn/Skins/UserLogin.ascx";
        private TextBox tbxLoginName;
        private TextBox tbxLoginPwd;
        private Button btnLogin;
        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        protected override void InitializeSkin(Control skin)
        {
            tbxLoginName = skin.FindControl<TextBox>("tbxLoginName");
            tbxLoginPwd = skin.FindControl<TextBox>("tbxLoginPwd");
            btnLogin = skin.FindControl<Button>("btnLogin");
            btnLogin.Click += new EventHandler(btnLogin_Click);
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            string strUsername = tbxLoginName.Text.Trim();
            string strPwd = Ue.Framework.Security.DEncryptUtils.MD5EnCode(tbxLoginPwd.Text.Trim());
            string struserId;
            int state = userLogin(strUsername, strPwd, out struserId);
            switch (state)
            {
                case -2: this.MessageWindow.RenderMessage("用户未激活");
                    break;
                case -1: this.MessageWindow.RenderMessage("密码错误");
                    break;
                case 0: this.MessageWindow.RenderMessage("用户不存在");
                    break;
                case 1:
                    string strUrl;
                    if (!SiteContext.UserInRole("admin", strUsername))
                    {
                        strUrl = "Default.aspx";
                        this.MessageWindow.RenderMessage("登陆成功", strUrl);
                    }
                    else
                    {
                        strUrl=this.Page.Request["ReturnUrl"];
                        this.MessageWindow.RenderMessage("登陆成功", strUrl);
                    }
                    break;
                default: break;
                    

            }
        }
        private int userLogin(string strUsername, string strPwd,out string userId)
        {

            User user = Getuser(strUsername);
            if (user != null)
            {
                if (user.Password.Equals(strPwd))
                {
                    if (user.State == EntityState.Enable)
                    {
                        userId = user.Key.ToString();
                        user.CreateFormsAuthentication();
                        return 1;//登录成功
                    }
                    else
                    {
                        userId = null;
                        return -2;//未激活
                    }
                }
                else
                {
                    userId = null;
                    return -1;//密码错误

                }
            }
            else
            {
                userId = null;
                return 0;//用户不存挂在
            }
        }

        private User Getuser(string userName)
        {
            IUserRepository repository =
                Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
            User user = repository.FindBy(userName);
            return user;
        }

    }
}
